package com.zl.autocode.controller.autocode;


import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zl.autocode.controller.base.AutocodeBaseController;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlAddDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlDelDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlDetailDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlEditDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlQueryDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlStateDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlAddDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlDelDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlDetailDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlEditDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlQueryDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlStateDTO;
import com.zl.autocode.service.autocode.IKeywordDtlService;
import com.zl.common.aop.LogSimpleResult;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.ResDTO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 数据库关键字明细表
 * @author ZL
 */
@Api(value="数据库关键字明细表",tags ="数据库关键字明细表")
@Controller("autocodeKeywordDtlController")
@RequestMapping("/autocode/keywordDtl")
public class KeywordDtlController extends AutocodeBaseController {

    @Autowired
    private IKeywordDtlService keywordDtlService;
    
    /**
     * 查询
     */
    @ApiOperation(value="数据库关键字明细表-查询")
    @ResponseBody
    @RequestMapping(value="/list", method = RequestMethod.POST)
    public ResDTO<ResKeywordDtlQueryDTO> listKeywordDtl(@RequestBody ReqDTO<ReqKeywordDtlQueryDTO> reqInfo) throws Exception{
        ResKeywordDtlQueryDTO data = keywordDtlService.queryKeywordDtlList(reqInfo);
        return successDATA(data);
    }
    
   /**
     * 详情
     */
    @ApiOperation(value="数据库关键字明细表-详情")
    @ResponseBody
    @RequestMapping(value="/detail", method = RequestMethod.POST)
    public ResDTO<ResKeywordDtlDetailDTO> detailKeywordDtl(@RequestBody ReqDTO<ReqKeywordDtlDetailDTO> reqInfo) throws Exception{
        ResKeywordDtlDetailDTO data = keywordDtlService.queryKeywordDtlDetail(reqInfo);
        return successDATA(data);
    }
    
    /**
     * 新增
     */
    @ApiOperation(value="数据库关键字明细表-新增")
    @ResponseBody
    @RequestMapping(value="/add", method = RequestMethod.POST)
        public ResDTO<ResKeywordDtlAddDTO> addKeywordDtl(@RequestBody ReqDTO<ReqKeywordDtlAddDTO> reqInfo) throws Exception{
        ResKeywordDtlAddDTO data = keywordDtlService.insertKeywordDtlInfo(reqInfo);
        return successDATA(data);
    }
    
    /**
     * 编辑
     */
    @ApiOperation(value="数据库关键字明细表-编辑")
    @ResponseBody
    @RequestMapping(value="/edit", method = RequestMethod.POST)
        public ResDTO<ResKeywordDtlEditDTO> editKeywordDtl(@RequestBody ReqDTO<ReqKeywordDtlEditDTO> reqInfo) throws Exception{
        ResKeywordDtlEditDTO data = keywordDtlService.updateKeywordDtlInfo(reqInfo);
        return successDATA(data);
    }
    
    /**
     * 删除
     */
    @ApiOperation(value="数据库关键字明细表-删除")
    @ResponseBody
    @RequestMapping(value="/del", method = RequestMethod.POST)
        public ResDTO<ResKeywordDtlDelDTO> delKeywordDtl(@RequestBody ReqDTO<ReqKeywordDtlDelDTO> reqInfo) throws Exception{
        ResKeywordDtlDelDTO data = keywordDtlService.deleteKeywordDtlInfo(reqInfo);
        return successDATA(data); 
    }
    
    /**
     * 状态更新
     */
    @ApiOperation(value="数据库关键字明细表-状态更新")
    @ResponseBody
    @RequestMapping(value="/state", method = RequestMethod.POST)
        public ResDTO<ResKeywordDtlStateDTO> stateKeywordDtl(@RequestBody ReqDTO<ReqKeywordDtlStateDTO> reqInfo) throws Exception{
        ResKeywordDtlStateDTO data = keywordDtlService.updateKeywordDtlState(reqInfo);
        return successDATA(data); 
    }
    
    @ResponseBody
    @LogSimpleResult
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResDTO<ResKeywordDtlAddDTO> uploadKeywordDtl(String keywordId, MultipartFile fileObj) throws Exception {
        ResKeywordDtlAddDTO resDTO = new ResKeywordDtlAddDTO();
        
        if(fileObj==null || fileObj.isEmpty()){
            throw new BizException(GlobalRespEnum.MISSING_FILE);
        }
        String fileExt = FileUtil.extName(fileObj.getResource().getFilename());
        if(!"xls".equalsIgnoreCase(fileExt)  && !"xlsx".equalsIgnoreCase(fileExt) && !"txt".equalsIgnoreCase(fileExt) && !"csv".equalsIgnoreCase(fileExt)){
            throw new BizException(GlobalRespEnum.MISSING_FILE);
        }
        
        List<String> keys =new ArrayList<>();
        if("txt".equalsIgnoreCase(fileExt) || "csv".equalsIgnoreCase(fileExt)){
            File file = FileUtil.createTempFile();
            FileUtil.writeFromStream(fileObj.getInputStream(), file);
            keys = FileUtil.readUtf8Lines(file);
        }else{
            ExcelReader reader = ExcelUtil.getReader(fileObj.getInputStream(), 0);
            Sheet sheet = reader.getSheet();
            if(sheet!=null && sheet.getLastRowNum()>0){
               for(int i=0;i<sheet.getLastRowNum();i++){
                   Row row = sheet.getRow(i);
                   if(row!=null){
                      Cell cell0 = row.getCell(0);
                      if(cell0!=null&&StringUtils.isNotBlank(cell0.getStringCellValue())){
                          keys.add(cell0.getStringCellValue().trim());
                      }
                   }
               }
            }
        }
        
        keywordDtlService.doUploadKeywordDtl(keywordId,keys);
        
        return successDATA(resDTO);
    }
}